﻿#define _CRT_SECURE_NO_WARNINGS 1

//牛客网::DP41 【模板】01背包
#include <iostream>
#include <string.h>
using namespace std;

const int N = 1010;
int n, V, v[N], w[N];
int dp[N][N];

int main()
{
    // 读⼊数据
    cin >> n >> V;
    for (int i = 1; i <= n; i++)
        cin >> v[i] >> w[i];
    // 解决第⼀问
    for (int i = 1; i <= n; i++)
        for (int j = 0; j <= V; j++) // 修改遍历顺序
        {
            dp[i][j] = dp[i - 1][j];
            if (j >= v[i])
                dp[i][j] = max(dp[i][j], dp[i - 1][j - v[i]] + w[i]);
        }

    cout << dp[n][V] << endl;
    // 解决第⼆问
    memset(dp, 0, sizeof dp);
    for (int j = 1; j <= V; j++) dp[0][j] = -1;
    for (int i = 1; i <= n; i++)
        for (int j = 0; j <= V; j++) // 修改遍历顺序
        {
            dp[i][j] = dp[i - 1][j];
            if (j >= v[i] && dp[i - 1][j - v[i]] != -1)
                dp[i][j] = max(dp[i][j], dp[i - 1][j - v[i]] + w[i]);
        }

    cout << (dp[n][V] == -1 ? 0 : dp[n][V]) << endl;
    return 0;
}

//优化后代码：：
#include <iostream>
#include <string.h>
using namespace std;

const int N = 1010;
int n, V, v[N], w[N];
int dp[N];

int main()
{
    // 读⼊数据
    cin >> n >> V;
    for (int i = 1; i <= n; i++)
        cin >> v[i] >> w[i];
   
    // 解决第⼀问
    for (int i = 1; i <= n; i++)
        for (int j = V; j >= v[i]; j--) // 修改遍历顺序
            dp[j] = max(dp[j], dp[j - v[i]] + w[i]);
    cout << dp[V] << endl;
    
    // 解决第⼆问
    memset(dp, 0, sizeof dp);
    for (int j = 1; j <= V; j++) dp[j] = -1;
    for (int i = 1; i <= n; i++)
        for (int j = V; j >= v[i]; j--)
            if (dp[j - v[i]] != -1)
                dp[j] = max(dp[j], dp[j - v[i]] + w[i]);

    cout << (dp[V] == -1 ? 0 : dp[V]) << endl;
    return 0;
}



//力扣 416. 分割等和子集
class Solution {
public:
    bool canPartition(vector<int>& nums) {
        int sum = 0;
        for (auto& x : nums) sum += x;
        if (sum % 2 == 1)  return false;
        int m = nums.size();
        int aim = sum / 2; // 定义⼀下⽬标值
        vector<vector<bool>> dp(m + 1, vector<bool>(aim + 1));

        for (int i = 0; i <= m; i++)
            dp[i][0] = true;

        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= aim; j++)
            {
                dp[i][j] = dp[i - 1][j];
                if (j >= nums[i - 1])  //因为多加了一行所有要i-1
                    dp[i][j] = dp[i][j] || dp[i - 1][j - nums[i - 1]];
            }
        }

        return dp[m][aim];
    }
};

//优化后::
class Solution {
public:
    bool canPartition(vector<int>& nums) {
        int sum = 0;
        for (auto& x : nums) sum += x;
        if (sum % 2 == 1)  return false;
        int m = nums.size();
        int aim = sum / 2; // 定义⼀下⽬标值
        vector<bool> dp(aim + 1);

        for (int i = 0; i <= m; i++)
            dp[0] = true;

        for (int i = 1; i <= m; i++)
        {
            for (int j = aim; j >= nums[i - 1]; j--)
            {
                dp[j] = dp[j] || dp[j - nums[i - 1]];
            }
        }

        return dp[aim];
    }
};


//力扣 494. 目标和
class Solution {
public:
    int findTargetSumWays(vector<int>& nums, int target) {
        //  sum
    // (正)a   (负)b->绝对值
    // a + b = sum
    // a - b = target
    // 2a = sum + target
    // a = (sum+target)/2

        int sum = 0;
        int m = nums.size();
        for (auto& x : nums)
            sum += x;

        int aim = (sum + target) / 2;
        if (aim < 0 || (sum + target) % 2 == 1)
            //a如果是小于0或者sum + target模不尽是0.几的的直接返回
            return 0;

        vector<vector<int>> dp(m + 1, vector<int>(aim + 1));
        dp[0][0] = 1;

        for (int i = 1; i <= m; i++)
        {
            for (int j = 0; j <= aim; j++)
            {
                dp[i][j] = dp[i - 1][j];
                if (nums[i - 1] <= j)
                    dp[i][j] += dp[i - 1][j - nums[i - 1]];
            }
        }

        return dp[m][aim];

    }
};

//优化后::
class Solution {
public:
    int findTargetSumWays(vector<int>& nums, int target) {
        //  sum
    // (正)a   (负)b->绝对值
    // a + b = sum
    // a - b = target
    // 2a = sum + target
    // a = (sum+target)/2

        int sum = 0;
        int m = nums.size();
        for (auto& x : nums)
            sum += x;

        int aim = (sum + target) / 2;
        if (aim < 0 || (sum + target) % 2 == 1)
            //a如果是小于0或者sum + target模不尽是0.几的的直接返回
            return 0;

        vector<int> dp(aim + 1);
        dp[0] = 1;

        for (int i = 1; i <= m; i++)
        {
            for (int j = aim; j >= nums[i - 1]; j--)
            {
                dp[j] += dp[j - nums[i - 1]];
            }
        }

        return dp[aim];

    }
};


//力扣1049. 最后一块石头的重量 II
class Solution {
public:
    int lastStoneWeightII(vector<int>& stones) {
        int sum = 0;
        int m = stones.size();
        for (auto& x : stones) sum += x;
        int aim = sum / 2;

        vector<vector<int>> dp(m + 1, vector<int>(aim + 1));

        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= aim; j++)
            {
                dp[i][j] = dp[i - 1][j];
                if (j >= stones[i - 1])
                {
                    dp[i][j] = max(dp[i][j], dp[i - 1][j - stones[i - 1]] + stones[i - 1]);
                }
            }
        }

        return sum - dp[m][aim] - dp[m][aim];
    }
};

//优化::
class Solution {
public:
    int lastStoneWeightII(vector<int>& stones) {
        int sum = 0;
        int m = stones.size();
        for (auto& x : stones) sum += x;
        int aim = sum / 2;

        vector<int> dp(aim + 1);

        for (int i = 1; i <= m; i++)
        {
            for (int j = aim; j >= stones[i - 1]; j--)
            {
                dp[j] = max(dp[j], dp[j - stones[i - 1]] + stones[i - 1]);

            }
        }

        return sum - dp[aim] - dp[aim];
    }
};


//322. 零钱兑换
class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        int m = coins.size();
        vector<vector<int>> dp(m + 1, vector<int>(amount + 1));
        for (int j = 1; j <= amount; j++)
            dp[0][j] = 0x3f3F3F3F;

        for (int i = 1; i <= m; i++)
        {
            for (int j = 0; j <= amount; j++)
            {
                dp[i][j] = dp[i - 1][j];
                if (j >= coins[i - 1] && dp[i][j - coins[i - 1]] != 0x3f3F3F3F)
                    dp[i][j] = min(dp[i][j], dp[i][j - coins[i - 1]] + 1);
            }
        }

        /*for(int i = 0; i <= m ; i++)
        {
            for(int j = 0; j<=amount; j++)
            {

              cout << dp[i][j] << " ";
            }
            cout << endl;
        }*/


        return dp[m][amount] == 0x3f3F3F3F ? -1 : dp[m][amount];
    }
};

//优化后代码
class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        int m = coins.size();
        vector<int> dp(amount + 1, 0x3f3f3f3f);
        dp[0] = 0;
        for (int i = 1; i <= m; i++)
        {
            for (int j = coins[i - 1]; j <= amount; j++)
            {
                dp[j] = min(dp[j], dp[j - coins[i - 1]] + 1);
            }
        }
        return dp[amount] == 0x3f3F3F3F ? -1 : dp[amount];
    }
};


//完全背包问题
#include <iostream>
#include <string.h>
using namespace std;
const int N = 1010;
int n, V, v[N], w[N];
int dp[N][N];
int main()
{
    // 读⼊数据
    cin >> n >> V;
    for (int i = 1; i <= n; i++)
        cin >> v[i] >> w[i];

    // 搞定第⼀问
    for (int i = 1; i <= n; i++)
        for (int j = 0; j <= V; j++)
        {
            dp[i][j] = dp[i - 1][j];
            if (j >= v[i]) dp[i][j] = max(dp[i][j], dp[i][j - v[i]] + w[i]);
        }

    cout << dp[n][V] << endl;

    // 第⼆问
    memset(dp, 0, sizeof dp);

    //初始化
    for (int j = 1; j <= V; j++) dp[0][j] = -1;

    for (int i = 1; i <= n; i++)
        for (int j = 0; j <= V; j++)
        {
            dp[i][j] = dp[i - 1][j];
            if (j >= v[i] && dp[i][j - v[i]] != -1)
                dp[i][j] = max(dp[i][j], dp[i][j - v[i]] + w[i]);
        }
    cout << (dp[n][V] == -1 ? 0 : dp[n][V]) << endl;

    return 0;
}


//优化
#include <iostream>
#include <string.h>
using namespace std;
const int N = 1010;
int n, V, v[N], w[N];
int dp[N];
int main()
{
    // 读⼊数据
    cin >> n >> V;
    for (int i = 1; i <= n; i++)
        cin >> v[i] >> w[i];

    // 搞定第⼀问
    for (int i = 1; i <= n; i++)
        for (int j = v[i]; j <= V; j++)
            dp[j] = max(dp[j], dp[j - v[i]] + w[i]);

    cout << dp[V] << endl;
    // 第⼆问
    memset(dp, 0, sizeof dp);
    for (int j = 1; j <= V; j++) dp[j] = -0x3f3f3f3f;
    for (int i = 1; i <= n; i++)
        for (int j = v[i]; j <= V; j++)
            dp[j] = max(dp[j], dp[j - v[i]] + w[i]);

    cout << (dp[V] < 0 ? 0 : dp[V]) << endl;
    return 0;
}